package office.action.person;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import office.action.person.vo.TableVo;
import office.bo.person.Table;
import office.service.person.IPersonService;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import current.security.user.domain.User;
import current.security.user.manager.service.UserManagerService;
import platform.common.action.BaseAction;
@Namespace("/table")
@ParentPackage("struts-base")
@Controller("tableAction")
@Scope("prototype")
public class TableAction extends BaseAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private TableVo tableVo;
	
	private String beginDate;
	
	private String endDate;
	
	private List<TableVo> listTableVo = new ArrayList<TableVo>();
	private UserManagerService userManagerService;
	private List<User> users = new ArrayList<User>();
	@Resource
	private IPersonService personalServiceImpl;
	
	@Action(value="gotoTable",results={@Result(location="/pages/table/tableEdit.jsp")})
	public String gotoTable(){
		return "success";
	}
	
	@Action(value="saveTable")
	public String saveTable(){
		try {
			Table table = personalServiceImpl.getTableById(tableVo.getId());
			if(tableVo != null && tableVo.getId() != null && tableVo.getId().equals("")){
				tableVo.setId(null);
			}
			if(table == null){
				table = new Table();
				table.setUser(tableVo.getUser());
			}
			table.setCustomer(tableVo.getCustomer());
			table.setMoney(tableVo.getMoney());
			table.setOutDate(tableVo.getOutDate());
			table.setProjectName(tableVo.getProjectName());
			table.setId(tableVo.getId());
			personalServiceImpl.saveTable(table);
			this.getAjaxMessagesJson().setMessage(true, "", "编辑成功");
		} catch (Exception e) {
			log.error("", e);
			this.getAjaxMessagesJson().setMessage(true, "", "编辑失败");
		}
		return RESULT_AJAXJSON;
	}
	
	@Action(value="gotoTableMenu",results={@Result(location="/pages/table/tableXml.jsp")})
	public String gotoTableMenu(){
		return "success";
	}
	
	@Action(value="gotoDeleteTable",results={@Result(location="/pages/table/tableDel.jsp")})
	public String gotoDeleteTable(){
		return "success";
	}
	
	@Action(value="deleteTableById")
	public String deleteTableById(){
		personalServiceImpl.deleteTableById(tableVo.getId());
		this.getAjaxMessagesJson().setMessage(true, "", "删除成功");
		return RESULT_AJAXJSON;
	}
	
	@Action(value="getTableById",results={@Result(location="/pages/table/tableEdit.jsp")})
	public String getTableById(){
		try {
			Table table = personalServiceImpl.getTableById(tableVo.getId());
			BeanUtils.copyProperties(tableVo ,table);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "success";
	}
	@Action(value="getAllTableByDate",results={@Result(location="/pages/table/table.jsp")})
	public String getAllTableByDate(){
		String userId = null;
		if(tableVo != null && tableVo.getUser() != null){
			userId = tableVo.getUser().getId();
		}
		getAllUser();
		List<Table> list = personalServiceImpl.getAllTableByDate(userId,beginDate, endDate);
		for (Iterator<Table> iterator = list.iterator(); iterator.hasNext();) {
			Table table = (Table) iterator.next();
			TableVo tableVo = new TableVo();
			try {
				BeanUtils.copyProperties(tableVo, table);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
			listTableVo.add(tableVo);
		}
		return "success";
	}
	
	public String getAllUser(){
		users = userManagerService.getAllUser();
		return null;
	}
	
	 
	public IPersonService getPersonalServiceImpl() {
		return personalServiceImpl;
	}

	public void setPersonalServiceImpl(IPersonService personalServiceImpl) {
		this.personalServiceImpl = personalServiceImpl;
	}

	public TableVo getTableVo() {
		return tableVo;
	}

	public void setTableVo(TableVo tableVo) {
		this.tableVo = tableVo;
	}

	public String getBeginDate() {
		return beginDate;
	}

	public void setBeginDate(String beginDate) {
		this.beginDate = beginDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public List<TableVo> getListTableVo() {
		return listTableVo;
	}

	public void setListTableVo(List<TableVo> listTableVo) {
		this.listTableVo = listTableVo;
	}

	public UserManagerService getUserManagerService() {
		return userManagerService;
	}

	public void setUserManagerService(UserManagerService userManagerService) {
		this.userManagerService = userManagerService;
	}

	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

}
